package cn.iocoder.yudao.module.member.alliance.service;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.business.eligibility.service.AppEligibilityBaseService;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.member.alliance.enums.CloudAllianceTypeEnum;
import cn.iocoder.yudao.module.api.member.alliance.vo.CloudAllianceApplyVO;
import cn.iocoder.yudao.module.member.alliance.dal.dataobject.CloudAllianceApplyDO;
import cn.iocoder.yudao.module.member.alliance.dal.dataobject.CloudAllianceOrderDO;
import cn.iocoder.yudao.module.member.alliance.dal.dataobject.CloudAllianceUserDO;
import cn.iocoder.yudao.module.member.alliance.dal.mysql.CloudAllianceUserMapper;
import cn.iocoder.yudao.module.member.alliance.mq.CloudAllianceProducer;
import cn.iocoder.yudao.module.member.alliance.vo.AppCloudAllianceUserVO;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 云盟商信息 Service 实现类
 *
 * @author 山野羡民
 */
@Slf4j
@Service
@Validated
public class AppCloudAllianceUserServiceImpl extends AppEligibilityBaseService<CloudAllianceUserDO, CloudAllianceOrderDO, AppCloudAllianceUserVO> implements AppCloudAllianceUserService {
    @Resource
    private CloudAllianceUserMapper cloudAllianceUserMapper;

    @Resource
    private CloudAllianceProducer cloudAllianceProducer;

    @Override
    protected CloudAllianceUserMapper getInjectedMapper() {
        return cloudAllianceUserMapper;
    }

    @Nonnull
    @Override
    protected CloudAllianceUserDO buildForInsert(@Nonnull Long userId) {
        return new CloudAllianceUserDO();
    }

    @Nonnull
    @Override
    protected CloudAllianceUserDO buildForUpdate(@Nonnull CloudAllianceOrderDO order, @Nonnull CloudAllianceUserDO aDo) {
        CloudAllianceUserDO cloudAllianceUserDO = super.buildForUpdate(order, aDo);
        cloudAllianceUserDO.setType(order.getType());
        return cloudAllianceUserDO;
    }

    @Override
    public boolean isValid(Long userId, @Nullable CloudAllianceTypeEnum type) {
        try {
            CloudAllianceUserDO allianceUserDO = getOrCreate(userId);
            if (type == null) {
                return allianceUserDO.isValid();
            }
            return allianceUserDO.isValid() && Objects.equals(type.getValue(), allianceUserDO.getType());
        } catch (Exception e) {
            log.error("isValid error", e);
            return false;
        }
    }

    @Override
    public void updateWhenAuditPassed(CloudAllianceApplyDO aDo) {
        CloudAllianceUserDO allianceUserDO = getOrCreate(aDo.getUserId());
        CloudAllianceUserDO updateObj = new CloudAllianceUserDO();
        updateObj.setId(allianceUserDO.getId());
        updateObj.setContactName(aDo.getContactName());
        updateObj.setContactMobile(aDo.getContactMobile());
        getInjectedMapper().updateById(updateObj);
        cloudAllianceProducer.sendCloudAllianceAuditPassMessage(BeanUtils.toBean(aDo, CloudAllianceApplyVO.class));
    }
    @Override
    public int cloudAllianceUserExpireBySystem() {
        CoreLambdaQueryWrapper<CloudAllianceUserDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.ne(CloudAllianceUserDO::getType, CloudAllianceTypeEnum.NORMAL.getValue());
        wrapper.le(CloudAllianceUserDO::getExpireTime, LocalDateTime.now());
        List<CloudAllianceUserDO> list = getInjectedMapper().selectList(wrapper);
        if (CollUtil.isEmpty(list)) {
            return 0;
        }
        List<CloudAllianceUserDO> updateList = new ArrayList<>();
        for (CloudAllianceUserDO cloudAllianceUserDO : list) {
            cloudAllianceProducer.sendCloudAllianceUserExpiredMessage(cloudAllianceUserDO.getUserId());
            CloudAllianceUserDO updateObj = new CloudAllianceUserDO();
            updateObj.setId(cloudAllianceUserDO.getId());
            updateObj.setType(CloudAllianceTypeEnum.NORMAL.getValue());
            updateList.add(updateObj);
        }
        getInjectedMapper().updateById(updateList);
        return updateList.size();
    }

}